import AntModal from "@/components/pt/dialog/AntModal";
import UploadFile from "@/components/pt/uploadfile/Index.vue";
import {
  getBizLianzhengJianshe,
  addBizLianzhengJianshe,
  updateBizLianzhengJianshe,
  findMaxSort,
} from "@/api/biz/bizLianzhengJianshe";

export default {
  name: "CreateForm",
  props: {
    fileTypeOptions: {
      type: Array,
      required: true,
    },

    typeOptions: {
      type: Array,
      required: true,
    },
    type: {
      type: String,
      required: true,
    },
    deptOptions: {
      type: Array,
      required: true,
    },
  },
  components: {
    AntModal,
    UploadFile,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加廉政建设",
      // 表单参数
      form: {},
      yearShowOne: false,
      rules: {
        name: [
          { required: true, message: "文件名称不能为空", trigger: "blur" },
        ],

        counts: [{ required: true, message: "数量不能为空", trigger: "blur" }],

        fileType: [
          {
            required: true,
            message: "类型不能为空",
            trigger: "blur",
          },
        ],

        type: [
          { required: true, message: "建设类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    openChangeOne(status) {
      if (status) {
        this.yearShowOne = true;
      }
    },
    // 得到年份选择器的值
    panelChangeOne(value) {
      this.form.times = value;
      this.yearShowOne = false;
    },
    fileOk(file) {
      console.log(file);
      this.form.fileName = file.file.response.data.fileName;
      this.form.fileUrl = file.file.response.data.url;

      this.$message.success("上传成功");
    },
    onTreeChange(v, label) {
      console.log("onchange", v, label);
      this.form.deptId = v;
      this.form.deptName = label[0];
    },
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,

        name: undefined,

        fileName: undefined,

        counts: undefined,

        fileUrl: undefined,

        fileType: undefined,

        type: undefined,

        sort: undefined,

        status: undefined,

        remark: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd(id) {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "新增" + this.type;
        this.id = id;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizLianzhengJiansheId = row.id;
      getBizLianzhengJianshe(bizLianzhengJiansheId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改廉政建设";
        this.spinning = !this.spinning;
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            const saveForm = JSON.parse(JSON.stringify(this.form));
            updateBizLianzhengJianshe(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            this.form.id = this.id;
            this.form.type = this.type;
            const saveForm = JSON.parse(JSON.stringify(this.form));
            addBizLianzhengJianshe(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/lianzhengjianshe/index";
      this.$router.push(index);
    },
  },
};
